module.exports = {
	data() {
	    return {
			initParam:{
				weixinLogin:false
			},
			load:false,
			plugin:{},
			curPage:false,
			curPageName:"",
			cur_page_full:"",
			param:{},
			data:null,//页面初始加载数据
			navSet:{
				is_show:1,
				title:"",
				weizhi:'center',
				showType:2,
				bgColor:['#ffffff'],
				searchBox:0,
				searchBc:'#ffffff',
				searchName:"",
				placeholderColor:"#999999",
				searchBorderColor:"#cccccc",
				direction:'right',
				titleColor:"#000000",
				icon:"",
				showIcon:0,
				iconPage:"",
				
			},
			tabbar:{
				curPage:false,
				height:0
			},
			pageStyle:'',//带固定高度
			pageStyleAuto:'',//这个是不带固定高度的
			pageClass:"moxi-page-box",
			pagePower:[],//页面访问权限
			modData:[],
			appType:"",
			pageReady:false,
			sys:{//当前设备信息，相当于uni.getSystemInfo
				navHeight:0
			},
			pageHeight:0,//当前页面可用高度,
			app_color:"",
			navHeight:0,
			
			userinfo:null,
			moxiData:{
				loginBox:false
			},
			curPagePassword:"",//当前设置的密码
			pagePasswordSuccess:false,//密码是否正确
			colorList:[]
		
	    }
	},
	async onShareAppMessage() {
		let data = await this.getShareInfo(uni.$u.mpShare);
		// console.log('分享',data);
		return data;
    },
    async onLoad(param) {
		
		this.initPage(param);
    },onShow() {
		
	},onHide(){
		 
	},onUnload(){
	},methods:{
		initPage(param){
			this.userinfo = this.getUserInfo();
			let self = this;
			this.cur_page_full = this.get_cur_page();
			// #ifdef MP-WEIXIN
			uni.showShareMenu({
				withShareTicket: true,
				  //设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
				  menus: ["shareAppMessage"
				  , "shareTimeline"
				  ]
			});
			// #endif
			
			uni.$u.mpShare = {
			    title: '', // 默认为小程序名称
			    path: '', // 默认为当前页面路径
			    imageUrl: '' // 默认为当前页面的截图
			}
			
			if(param.appid){
				param.appid = Number(param.appid);
				this.setAppid(param.appid)
			}
			if(param.module){
				param.module = Number(param.module);
				this.setModule(param.module)
			}
			
			this.sys = uni.$u.sys();
			this.pageHeight = this.sys.windowHeight;
			this.appType = this.moxiConfig('appType');
			this.$appData.app_type = this.appType;
			
			this.param = JSON.parse(JSON.stringify(param));
			this.$pageData.param = this.param;
			if(this.param.reuid){//保存推荐用户ID
				uni.setStorageSync('reuid',this.param.reuid);
				this.$appData.reuid = this.param.reuid;
			}
			//自动用户登录
			// if(!this.userinfo){
			// 	if(this.appType != 'h5' && this.appType != 'app'){
			// 		uni.login({
			// 		  provider: this.appType, //使用微信登录
			// 		  success (loginRes){
			// 			self.moxiPost('/index/app/login', {unicode:loginRes.code},  (data)=> {
			// 				self.getConfig();
			// 			})
			// 		  },fail(e) {
			// 			self.getConfig();
			// 			// self.msg(e.errMsg)
			// 		  }
			// 		});
			// 	}else{
			// 		this.getConfig();
			// 	}
			// }else{
			// 	this.getConfig();
			// }
			this.getConfig();
		},
		getShareInfo(mpShare) {
			return new Promise((resolve, reject) => {
				this.moxiPost('/index/app/get_share',{page:this.get_cur_page(),myShare:mpShare},function(data){
					resolve(data.data)
				})
			})
		},
		moxiLength(ob){
			let num = 0;
			for(let k in ob){
				num++;
			}
			return num;
		},
		getConfig(){
			let pages = getCurrentPages();//页面示例
			this.curPage = pages[pages.length - 1].route;//当前页面实例
			if (this.curPage.startsWith('/')) {
				this.curPage =  this.curPage.substring(1);
			}
			
			if(this.curPage=='pages/index/index'){
				if(!this.param.id){
					this.param.id = 0;
					this.curPageName = 'index';
				}else{
					this.param.id = Number(this.param.id);
					this.curPageName = Number(this.param.id);
				}
			}else{
				this.curPageName = this.curPage;
			}
			this.$pageData.curPageName = this.curPageName;
			
			
			if(this.$appData.appid==this.moxiConfig('appid')&&this.$appData.module==this.moxiConfig('module')){
				this.configInit(this.$appData.data);
			}else{
				var self = this;
				this.moxiPost('/index/app/get_config',{},(data)=>{
					self.$appData.init =  true;//完成初始化
					self.$appData.appid = self.moxiConfig('appid');
					self.$appData.module = self.moxiConfig('module');
					self.$appData.data = data.data;
					self.configInit(self.$appData.data);
				},false)
			}
		},
		configInit(data){
			// console.log('页面配置',data.navSet);
			// console.log('当前完整地址：',this.cur_page_full);
			//优先使用完整页面配置
			for(let k in data.navSet){
				if(k.indexOf(this.cur_page_full)===0){
					this.curPageName = k;
					break;
				}
			}
			this.plugin = data.plugin;
			this.app_color = this.$appData.data.app_color;
			this.colorList = this.$appData.data.color;
			this.$appData.plugin = data.plugin;
			
			if(data.pagePower){//验证页面权限
				if(data.pagePower[this.curPageName]){
					if(data.pagePower[this.curPageName].length){
						if(!this.userinfo||!this.userinfo.gid){
							this.toLogin();return;
						}else{
							if(data.pagePower[this.curPageName].indexOf(this.userinfo.gid)){
								this.msg("您所在用户组无权访问");return;
							}
						}
					}
				}
			}
			if(data.pagePassword){//验证页面权限
				if(data.pagePassword[this.curPageName]&&data.pagePassword[this.curPageName].length){
					this.curPagePassword = data.pagePassword[this.curPageName];
					
					const ps = this.moxiCache('page_password_'+this.curPageName);
					if(ps===this.curPagePassword){
						this.pagePasswordSuccess = true;
					}
				}
			}
			this.navSet = data.navSet[this.curPageName];
		
			if(this.param.page_title){
				this.navSet.title = this.param.page_title;
			}
			
			if(data.pageStyle[this.curPageName]){
				this.pageStyle = data.pageStyle[this.curPageName];
			}
		
			if(data.pageAnimate[this.curPageName]&&data.pageAnimate[this.curPageName].length){
				this.pageClass = 'moxi-page-box animated '+data.pageAnimate[this.curPageName]
			}
			this.modData = [];
			if(data.modData[this.curPageName]){
				this.modData = data.modData[this.curPageName];
			}
	
			
			this.tabbar = JSON.parse(JSON.stringify(data.tabbar));
			this.tabbar.curKey = null;//当前页面的key
			this.tabbar.curPage = false;//tabbar中是否含有当前页面
			for(let k in this.tabbar.list){
				if(this.tabbar.curKey===null){
					if(this.tabbar.list[k].path==this.cur_page_full){
						this.tabbar.curKey = Number(k);
						if(this.tabbar.curPage===false){
							this.tabbar.curPage = true;
							this.tabbar.height = 50;
							this.pageHeight = this.pageHeight-50;
						}
					}
				}
				// if(this.tabbar.curPage===false){
				// 	if(this.tabbar.list[k].path==this.curPage){
				// 		// this.pageHeight = this.pageHeight-50;
				// 		this.tabbar.curPage = true;
				// 		this.tabbar.height = 50;
				// 	}
				// }
			}
			// console.log('curPage',String(this.tabbar.curPage)); 
			this.$appData.hasTabbar = this.tabbar.curPage;
			// console.log('初始化数据',data);
			// console.log('当前页面',this.curPageName);
			if(data.navSet[this.curPageName]){ 
				// console.log('导航设置',this.navSet);
				uni.setNavigationBarTitle({
					title:this.navSet.title
				});
				// uni.hideNavigationBarLoading()//此函数在小红书小程序报错
					if(this.navSet.is_show&&this.navSet.is_show==1){
						this.navHeight = this.sys.statusBarHeight+44;
					}else{
						this.navHeight =  this.sys.statusBarHeight;
					}
				
				
				this.$pageData.navHeight = this.navHeight;
				if(this.navSet.titleColor!='#ffffff'){
					this.navSet.titleColor = '#000000';
				}
				uni.setNavigationBarColor({
				    frontColor: this.navSet.titleColor,
				    backgroundColor: this.navSet.bgColor[0]
				})
			}
			this.pageStyleAuto = this.pageHeight;
			this.pageStyle += 'overflow: auto;position: relative;height: '+this.pageHeight+'px;';
			this.pageReady = true;
		}
	}
}
